/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package atm;

import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.StaticText;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version SelectPayee.java
 * @version Created on 2-Apr-2011, 10:58:28 PM
 * @author Albert
 */

public class SelectPayee extends ButtonsPage {
    private String sAccount;
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private Button btnPayee1 = new Button();

    public Button getBtnPayee1() {
        return btnPayee1;
    }

    public void setBtnPayee1(Button b) {
        this.btnPayee1 = b;
    }
    private StaticText stPayee1 = new StaticText();

    public StaticText getStPayee1() {
        return stPayee1;
    }

    public void setStPayee1(StaticText st) {
        this.stPayee1 = st;
    }
    private StaticText stPayeeAcc1 = new StaticText();

    public StaticText getStPayeeAcc1() {
        return stPayeeAcc1;
    }

    public void setStPayeeAcc1(StaticText st) {
        this.stPayeeAcc1 = st;
    }
    private Button btnPayee2 = new Button();

    public Button getBtnPayee2() {
        return btnPayee2;
    }

    public void setBtnPayee2(Button b) {
        this.btnPayee2 = b;
    }
    private StaticText stPayee2 = new StaticText();

    public StaticText getStPayee2() {
        return stPayee2;
    }

    public void setStPayee2(StaticText st) {
        this.stPayee2 = st;
    }
    private StaticText stPayeeAcc2 = new StaticText();

    public StaticText getStPayeeAcc2() {
        return stPayeeAcc2;
    }

    public void setStPayeeAcc2(StaticText st) {
        this.stPayeeAcc2 = st;
    }
    private Button btnPayee3 = new Button();

    public Button getBtnPayee3() {
        return btnPayee3;
    }

    public void setBtnPayee3(Button b) {
        this.btnPayee3 = b;
    }
    private StaticText stPayee3 = new StaticText();

    public StaticText getStPayee3() {
        return stPayee3;
    }

    public void setStPayee3(StaticText st) {
        this.stPayee3 = st;
    }
    private StaticText stPayeeAcc3 = new StaticText();

    public StaticText getStPayeeAcc3() {
        return stPayeeAcc3;
    }

    public void setStPayeeAcc3(StaticText st) {
        this.stPayeeAcc3 = st;
    }
    private Button btnPayee4 = new Button();

    public Button getBtnPayee4() {
        return btnPayee4;
    }

    public void setBtnPayee4(Button b) {
        this.btnPayee4 = b;
    }
    private StaticText stPayee4 = new StaticText();

    public StaticText getStPayee4() {
        return stPayee4;
    }

    public void setStPayee4(StaticText st) {
        this.stPayee4 = st;
    }
    private StaticText stPayeeAcc4 = new StaticText();

    public StaticText getStPayeeAcc4() {
        return stPayeeAcc4;
    }

    public void setStPayeeAcc4(StaticText st) {
        this.stPayeeAcc4 = st;
    }
    private Button btnPayee5 = new Button();

    public Button getBtnPayee5() {
        return btnPayee5;
    }

    public void setBtnPayee5(Button b) {
        this.btnPayee5 = b;
    }
    private StaticText stPayee5 = new StaticText();

    public StaticText getStPayee5() {
        return stPayee5;
    }

    public void setStPayee5(StaticText st) {
        this.stPayee5 = st;
    }
    private StaticText stPayeeAcc5 = new StaticText();

    public StaticText getStPayeeAcc5() {
        return stPayeeAcc5;
    }

    public void setStPayeeAcc5(StaticText st) {
        this.stPayeeAcc5 = st;
    }
    private Button btnPayee6 = new Button();

    public Button getBtnPayee6() {
        return btnPayee6;
    }

    public void setBtnPayee6(Button b) {
        this.btnPayee6 = b;
    }
    private StaticText stPayee6 = new StaticText();

    public StaticText getStPayee6() {
        return stPayee6;
    }

    public void setStPayee6(StaticText st) {
        this.stPayee6 = st;
    }
    private StaticText stPayeeAcc6 = new StaticText();

    public StaticText getStPayeeAcc6() {
        return stPayeeAcc6;
    }

    public void setStPayeeAcc6(StaticText st) {
        this.stPayeeAcc6 = st;
    }
    private Button btnPayee7 = new Button();

    public Button getBtnPayee7() {
        return btnPayee7;
    }

    public void setBtnPayee7(Button b) {
        this.btnPayee7 = b;
    }
    private StaticText stPayee7 = new StaticText();

    public StaticText getStPayee7() {
        return stPayee7;
    }

    public void setStPayee7(StaticText st) {
        this.stPayee7 = st;
    }
    private StaticText stPayeeAcc7 = new StaticText();

    public StaticText getStPayeeAcc7() {
        return stPayeeAcc7;
    }

    public void setStPayeeAcc7(StaticText st) {
        this.stPayeeAcc7 = st;
    }
    private Button btnPayee8 = new Button();

    public Button getBtnPayee8() {
        return btnPayee8;
    }

    public void setBtnPayee8(Button b) {
        this.btnPayee8 = b;
    }
    private StaticText stPayee8 = new StaticText();

    public StaticText getStPayee8() {
        return stPayee8;
    }

    public void setStPayee8(StaticText st) {
        this.stPayee8 = st;
    }
    private StaticText stPayeeAcc8 = new StaticText();

    public StaticText getStPayeeAcc8() {
        return stPayeeAcc8;
    }

    public void setStPayeeAcc8(StaticText st) {
        this.stPayeeAcc8 = st;
    }
    private Button btnPayee9 = new Button();

    public Button getBtnPayee9() {
        return btnPayee9;
    }

    public void setBtnPayee9(Button b) {
        this.btnPayee9 = b;
    }
    private StaticText stPayee9 = new StaticText();

    public StaticText getStPayee9() {
        return stPayee9;
    }

    public void setStPayee9(StaticText st) {
        this.stPayee9 = st;
    }
    private StaticText stPayeeAcc9 = new StaticText();

    public StaticText getStPayeeAcc9() {
        return stPayeeAcc9;
    }

    public void setStPayeeAcc9(StaticText st) {
        this.stPayeeAcc9 = st;
    }
    private Button btnPayee10 = new Button();

    public Button getBtnPayee10() {
        return btnPayee10;
    }

    public void setBtnPayee10(Button b) {
        this.btnPayee10 = b;
    }
    private StaticText stPayee10 = new StaticText();

    public StaticText getStPayee10() {
        return stPayee10;
    }

    public void setStPayee10(StaticText st) {
        this.stPayee10 = st;
    }
    private StaticText stPayeeAcc10 = new StaticText();

    public StaticText getStPayeeAcc10() {
        return stPayeeAcc10;
    }

    public void setStPayeeAcc10(StaticText st) {
        this.stPayeeAcc10 = st;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public SelectPayee() {
    }


    private String[] payeeNamesSt = new String[10];
    private String[] payeeAccount = new String[10];
    

    private void setPayeeVisible(int ibegin, int iend, boolean visible)
    {
       if (ibegin < 0) ibegin = 0;
       if (iend > 9) iend = 9;
       if (ibegin > 9) ibegin = 9;
       if (iend < ibegin) iend = ibegin;
       for (int i = ibegin; i<=iend; i++)
       {
           payeeNamesSt[i] = "";
           payeeAccount[i] = "";
          
       }

    }

    public String getPayee1Name()
    {
      return payeeNamesSt[0];
    }

    public String getPayee2Name()
    {
      return (String)payeeNamesSt[1];
    }

    public String getPayee3Name()
    {
      return (String)payeeNamesSt[2];
    }

    public String getPayee4Name()
    {
      return (String)payeeNamesSt[3];
    }

    public String getPayee5Name()
    {
      return (String)payeeNamesSt[4];
    }

    public String getPayee6Name()
    {
      return (String)payeeNamesSt[5];
    }

    public String getPayee7Name()
    {
      return (String)payeeNamesSt[6];
    }

    public String getPayee8Name()
    {
      return (String)payeeNamesSt[7];
    }

    public String getPayee9Name()
    {
      return (String)payeeNamesSt[8];
    }

    public String getPayee10Name()
    {
      return (String)payeeNamesSt[9];
    }

    public String getPayeeAcc1Name()
    {
      return (String)payeeAccount[0];
    }

    public String getPayeeAcc2Name()
    {
      return (String)payeeAccount[1];
    }

    public String getPayeeAcc3Name()
    {
      return (String)payeeAccount[2];
    }

    public String getPayeeAcc4Name()
    {
      return (String)payeeAccount[3];
    }

    public String getPayeeAcc5Name()
    {
      return (String)payeeAccount[4];
    }

    public String getPayeeAcc6Name()
    {
      return (String)payeeAccount[5];
    }

    public String getPayeeAcc7Name()
    {
      return (String)payeeAccount[6];
    }

    public String getPayeeAcc8Name()
    {
      return (String)payeeAccount[7];
    }

    public String getPayeeAcc9Name()
    {
      return (String)payeeAccount[8];
    }

    public String getPayeeAcc10Name()
    {
      return (String)payeeAccount[9];
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("SelectPayee Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
        UserCard = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.SessionVarCard);
        sAccount = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(Constants.SessionVarSelAccount1);
        //ba = BankAccount.getFromDB(UserCard, sAccount);
        String query = "Select * FROM " + Constants.PayeeTableName + " where card_number = " + UserCard;

        setPayeeVisible(0,9,false);
        
        ResultSet rs = Utils.GetQuerySet(query);
        String sPayee;
        String sAcc;
        if (rs == null)
        {

        }
        else
        try
        {
            int irow = 0;
            rs.first();
            while (!rs.isAfterLast()&& irow<10)
            {
                sPayee = rs.getString("Payee");
                payeeNamesSt[irow] = sPayee;
                
                sAcc = rs.getString("Account_Number");
                payeeAccount[irow] = sAcc;
                
                irow++;
                rs.next();
            }
        } 
        catch (SQLException ex)
        {

            Logger.getLogger(PINPage.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    @Override
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    @Override
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    @Override
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    @Override
    public String button0_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return "Pay_Bills";
    }

   
    @Override
    public String buttonCancel_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }

    private String getPayeeButtonAction(int i)
    {
        String result = null;
        if (!payeeNamesSt[i].isEmpty())
        {
          FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.SessionVarPayeeName, payeeNamesSt[i]);
          FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.SessionVarPayeeAccount, payeeAccount[i]);
          FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(Constants.SessionVarSelAccount1, sAccount);
          result = "Pay_Bills";
        }
        return result;
    }

    public String btnPayee1_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(0);
    }

    public String btnPayee2_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(1);
    }

    public String btnPayee3_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(2);
    }

    public String btnPayee4_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(3);
    }

    public String btnPayee5_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(4);
    }

    public String btnPayee6_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(5);
    }

    public String btnPayee7_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(6);
    }

    public String btnPayee8_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(7);
    }

    public String btnPayee9_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(8);
    }

    public String btnPayee10_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        return getPayeeButtonAction(9);
    }
    
}

